import * as THREE from "https://unpkg.com/three@0.119.0/build/three.module.js"

export class PickHelper {
    constructor() {
        this.raycaster = new THREE.Raycaster();
        this.pickedObject = null;
        this.pickedObjectSavedColor = 0;
    }
    pick(normalizedPosition, scene, camera) {
        // 恢复上一个被拾取对象的颜色
        if (this.pickedObject) {
            this.pickedObject.material.color.setHex(this.pickedObjectSavedColor);
            this.pickedObject = undefined;
        }

        // 发出射线
        this.raycaster.setFromCamera(normalizedPosition, camera);
        // 获取与射线相交的对象
        const intersectedObjects = this.raycaster.intersectObjects(scene.children);
        if (intersectedObjects.length) {
            // 找到第一个对象，它是离鼠标最近的对象
            this.pickedObject = intersectedObjects[0].object;
            // 保存它的颜色
            this.pickedObjectSavedColor = this.pickedObject.material.color.getHex();
            // 设置它的发光为 黄色/红色闪烁
            this.pickedObject.material.color.setHex(0xFFFF00);
        }
    }
}